﻿using JetBrains.ReSharper.Psi.CSharp;
using JetBrains.ReSharper.Psi.CSharp.Impl;
using JetBrains.ReSharper.Psi.CSharp.Parsing;
using JetBrains.ReSharper.Psi.CSharp.Tree;
using JetBrains.ReSharper.Psi.Parsing;
using JetBrains.Text;

namespace IntegrationTests.Core
{
    public static class CreaterTreeCSharpFile
    {
        public static ICSharpFile CreateTreeCSharpFile(string source)
        {
            var cSharpLanguageService = new CSharpLanguageService(CSharpLanguage.Instance);
            var buffer = new StringBuffer(source);
            ILexerFactory lex = cSharpLanguageService.GetPrimaryLexerFactory();
            var parser = (ICSharpParser) cSharpLanguageService.CreateParser(
                lex.CreateLexer(buffer), null, null, null);
            parser.ExpandChameleons = parser.OpenChameleonStrategy;
            return parser.ParseFile() as ICSharpFile;
        }
    }
}